//
//  HRPhoneLoginRootView.m
//  Hear
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRPhoneLoginRootView.h"
#import "HREnterTextFiledView.h"
#import "HRUpdataUserInfoModel.h"
#import <UMSocialCore/UMSocialCore.h>

@interface HRPhoneLoginRootView ()

@end
@implementation HRPhoneLoginRootView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"提示耳机图案"]];
        backImageView.contentMode = UIViewContentModeScaleAspectFill;
        backImageView.clipsToBounds = YES;
        [self addSubview:backImageView];
        backImageView.userInteractionEnabled = YES;

        

        
        
        self.phoneTextField = [HREnterTextFiledView new];
        [self addSubview:self.phoneTextField];
        self.phoneTextField.textField.placeholder = @"请输入手机号";
        [self.phoneTextField.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.phoneTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
        self.phoneTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;

        self.phoneTextField.textField.textColor = [UIColor whiteColor];
        self.phoneTextField.leftImageView.image = [UIImage imageNamed:@"手机"];
        
        

        self.passWordTextField = [HREnterTextFiledView new];
        [self addSubview:self.passWordTextField];
        self.passWordTextField.textField.placeholder = @"请输入密码";
        [self.passWordTextField.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.passWordTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
        self.passWordTextField.textField.secureTextEntry = YES;
        self.passWordTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.passWordTextField.leftImageView.image = [UIImage imageNamed:@"密码"];
        self.passWordTextField.textField.textColor = [UIColor whiteColor];
        
        UIButton *registButton = [UIButton buttonWithType:1];
        [registButton setTitle:@"注册" forState:UIControlStateNormal];
        [registButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        registButton.titleLabel.font = CustomUIFont(14);
        [registButton addTarget:self action:@selector(registButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:registButton];
        
        
        UIButton *forgetButton = [UIButton buttonWithType:1];
        [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
        [forgetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        forgetButton.titleLabel.font = CustomUIFont(14);
        [forgetButton addTarget:self action:@selector(forgetButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:forgetButton];
        
        
        UIButton *loginButton = [UIButton buttonWithType:1];
        [loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [loginButton setTitleColor:BlueColor forState:UIControlStateNormal];
        loginButton.titleLabel.font = CustomUIFont(17);
        [loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginButton];
        loginButton.layer.cornerRadius = 10;
        loginButton.clipsToBounds = YES;
        loginButton.backgroundColor = [UIColor whiteColor];

        
        
        UIButton *wxButton = [UIButton buttonWithType:0];
        [wxButton setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
        [wxButton addTarget:self action:@selector(otherLoginClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:wxButton];
        wxButton.tag = 0;
//        wxButton.hidden = ![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession];
        wxButton.hidden = ![[[NSUserDefaults standardUserDefaults]objectForKey:@"IsShowCash"] intValue];

        
//        UIButton *wbButton = [UIButton buttonWithType:0];
//        [wbButton setImage:[UIImage imageNamed:@"微博"] forState:UIControlStateNormal];
//        [wbButton addTarget:self action:@selector(otherLoginClick:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:wbButton];
//        wbButton.tag = 1;
//        wbButton.hidden = ![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_Sina];
//        wbButton.hidden = YES;
        
        UIButton *qqButton = [UIButton buttonWithType:0];
        [qqButton setImage:[UIImage imageNamed:@"QQ"] forState:UIControlStateNormal];
        [qqButton addTarget:self action:@selector(otherLoginClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:qqButton];
        qqButton.tag = 2;
//        qqButton.hidden = ![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];
        qqButton.hidden = ![[[NSUserDefaults standardUserDefaults]objectForKey:@"IsShowCash"] intValue];
//        BOOL ee = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];
        NSString *ddd = [[NSUserDefaults standardUserDefaults] objectForKey:@"IsShowCash"];
        
        UIButton *protocolButton = [UIButton buttonWithType:1];
        [protocolButton setTitle:@"注册即代表阅读并同意服务条款" forState:UIControlStateNormal];
        [protocolButton setTitleColor:CustomColor(160, 160, 160, 0.7) forState:UIControlStateNormal];
        protocolButton.titleLabel.font = CustomUIFont(13);
        [protocolButton addTarget:self action:@selector(protocolButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:protocolButton];
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:protocolButton.titleLabel.text];
        [att setColor:[UIColor whiteColor] range:NSMakeRange(10, 4)];
        protocolButton.titleLabel.attributedText = att;
        
        CGFloat Pading = 20;
        WeakSelf
        [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf);
        }];
        

        
        
        [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf).offset(-60);
            make.left.equalTo(weakSelf).offset(50);
            make.right.equalTo(weakSelf).offset(-50);
            make.height.mas_equalTo(41);
        }];
        
        [self.passWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.height.equalTo(weakSelf.phoneTextField);
            make.top.equalTo(weakSelf.phoneTextField.mas_bottom).offset(Pading);
        }];
        
        [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.passWordTextField);
            make.top.equalTo(weakSelf.passWordTextField.mas_bottom).offset(40);
            make.height.mas_equalTo(45);
        }];
        
        
        [registButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(loginButton.mas_left);
            make.top.equalTo(loginButton.mas_bottom).offset(15);
            make.size.mas_equalTo(CGSizeMake(30, 20));
        }];
        
        [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.height.equalTo(registButton);
            make.right.equalTo(loginButton);
            make.width.mas_equalTo(60);
        }];
        

        

        
        
        [wxButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(67, 67));
            make.centerX.equalTo(weakSelf).offset(-50);
            make.top.equalTo(forgetButton.mas_bottom).offset(20);
        }];
        
        //        [wbButton mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.size.mas_equalTo(CGSizeMake(40, 40));
        //            make.right.equalTo(wxButton.mas_left).offset(-20);
        //            make.top.equalTo(wxButton.mas_top);
        //        }];
        
        [qqButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(67, 67));
            make.centerX.equalTo(weakSelf).offset(50);
            make.top.equalTo(wxButton.mas_top);
        }];
        
        [protocolButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf);
            make.bottom.equalTo(weakSelf).offset(-30);
            make.size.mas_equalTo(CGSizeMake(200, 30));
            
        }];
        
    }
    return self;
}

#pragma -mark- 第三方登录
- (void)otherLoginClick:(id)sender
{
    if (self.otherClickBlock)
    {
        self.otherClickBlock([(UIButton *)sender tag]);
    }
}

- (void)registButtonClick
{
    if (self.registButtonClickBlock)
    {
        self.registButtonClickBlock();
    }
}

- (void)forgetButtonClick
{
    if (self.forgetButtonClickBlock)
    {
        self.forgetButtonClickBlock();
    }
}

- (void)loginButtonClick
{
    if (self.phoneTextField.textField.text.length != 11)
    {
        [MBProgressHUD showMessage:@"输入的手机号无效" finishBlock:nil];
        return;
    }
    if (self.passWordTextField.textField.text.length < 6)
    {
        [MBProgressHUD showMessage:@"密码长度不能小于6个字符" finishBlock:nil];
        return;
    }
    if (self.passWordTextField.textField.text.length > 20)
    {
        [MBProgressHUD showMessage:@"密码长度不能小于20个字符" finishBlock:nil];
        return;
    }
    
    if (self.loginButtonClickBlock)
    {
        self.loginButtonClickBlock();
    }
}

- (void)protocolButtonClick
{
    if (self.protocolButtonClickBlock)
    {
        self.protocolButtonClickBlock();
    }
}


@end
